import tushare as ts
from app import create_app, db
from app.models import Product

# 初始化 Flask 上下文
app = create_app()
app.app_context().push()

# 设置 Tushare Token
ts.set_token('bb9d2503efc80c5f3261ea30f69992d997254901f79a00d20b411e57')  # 请替换为你自己的 token
pro = ts.pro_api()

# 获取前10条股票数据
df = pro.stock_basic(exchange='', list_status='L', fields='ts_code,name,industry')
df = df.head(10)

# 插入到数据库
for _, row in df.iterrows():
    product = Product(
        code=row['ts_code'],
        name=row['name'],
        type='股票',
        industry=row['industry'],
    )
    db.session.add(product)

db.session.commit()
print("✅ 已成功导入10条产品数据")
